#include "qwdialogpen.h"
#include "ui_qwdialogpen.h"

QWDialogPen::QWDialogPen(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QWDialogPen)
{
    ui->setupUi(this);

    connect(ui->ColorBtn,&QPushButton::clicked,this,&QWDialogPen::onColorBtnClicked);

    // 确定按钮
    connect(ui->okBtn,&QPushButton::clicked,this,[=](){
        this->accept();
    });

    // 取消按钮
    connect(ui->cancelBtn,&QPushButton::clicked,this,[=](){
        this->reject();
    });
}

void QWDialogPen::setPen(QPen pen)
{
    m_pen = pen;
    ui->spinWidth->setValue(pen.width());
    int i = static_cast<int>(pen.style());
    ui->comboPenStyle->setCurrentIndex(i);
    QColor color = pen.color();
    ui->ColorBtn->setAutoFillBackground(true);
    QString str = QString::asprintf("background-color: rgb(%d, %d, %d);"
                                    ,color.red(),color.green(),color.blue());
    ui->ColorBtn->setStyleSheet(str);

}

QPen QWDialogPen::getPen()
{
    m_pen.setStyle(Qt::PenStyle(ui->comboPenStyle->currentIndex()));
    m_pen.setWidth(ui->spinWidth->value());
    QColor color = ui->ColorBtn->palette().color(QPalette::Button);
    m_pen.setColor(color);
    return m_pen;
}

QPen QWDialogPen::getPen(QPen initPen, bool &ok)
{
    // 静态函数，获取QPen
    QWDialogPen* Dlg = new QWDialogPen; // new对话框
    Dlg->setPen(initPen); // 设置初始化pen
    QPen pen;
    int ret = Dlg->exec(); // 弹出对话框
    if(ret == QDialog::Accepted){
        pen = Dlg->getPen(); // 获取pen
        ok = true;
    }
    else{
        pen = initPen;
        ok = false;
    }
    delete Dlg; // 删除对话框
    return pen;
}

void QWDialogPen::onColorBtnClicked()
{
    QColor color = ui->ColorBtn->palette().color(QPalette::Button);
    color = QColorDialog::getColor(color,nullptr,"获取颜色");
    QString str = QString::asprintf("background-color: rgb(%d, %d, %d);"
                                    ,color.red(),color.green(),color.blue());
    ui->ColorBtn->setStyleSheet(str);
}

QWDialogPen::~QWDialogPen()
{
    delete ui;
}
